package com.fingard.dsp.bank.directbank.hzcb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ReceiptProConfig;
import com.fingard.dsp.bank.directbank.bankInterface.IReceiptPro;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.ReqQryReceipt;
import com.fingard.dsp.bank.reqrespobj.downloadreceipt.RetAtsReceipt;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * @author Parker on 2020-06-03.
 * @version 1.0
 */
public class HZCB01ReceiptPro extends HZCB01Base implements IReceiptPro {
    @Override
    public RetAtsReceipt qryReceiptOneDay(ReceiptProConfig pConfig, ReqQryReceipt pReqReceipt, Date pOneDay) throws Exception {
        String qryDay8 = Format.Date8Format.format(pOneDay);
        //电子回单申请, 等待后续银行推送回单文件至SFTP
        //赋值通用参数
        txCode = "PY4101";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入通用的头部节点
        writeCommonHeader(xmlWriter);
        xmlWriter.writeStartElement("input");
        xmlWriter.writeElementString("compFlowNo", getQryTransID());
        xmlWriter.writeElementString("beginDate", qryDay8);
        xmlWriter.writeElementString("endDate", qryDay8);
        xmlWriter.writeStartElement("result");
        xmlWriter.writeStartElement("row");
        xmlWriter.writeElementString("recAccount", pReqReceipt.actNum);
        xmlWriter.writeEndElement();
        xmlWriter.writeEndElement();
        xmlWriter.writeEndElement();
        xmlWriter.writeEndElement();
        String signXml = getDataSignData(xmlWriter);
        if (signnatrue(signXml) == true) {
            xmlWriter.writeElementString("signData", signature);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(getDataSendBank(xmlWriter.toXmlString()),"DownloadReceipt","");
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpStatus = tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retCode");
                    String tmpStatusText = tmpBkReader.getFirstText("/HZBANKB2C/ebank/common/retMsg");
                    if (tmpStatus != null && isActiveRetCode(tmpStatus)) {
                        if ("90".equals(tmpBkReader.getFirstText("/HZBANKB2C/ebank/output/orderSTT"))) {
                            WriteBankLogLn("回单申请成功, 请等待推送数据");
                        } else {
                            WriteBankLogLn("回单申请处理状态:" + tmpBkReader.getFirstText("/HZBANKB2C/ebank/output/orderSRR"));
                        }
                    } else {
                        WriteBankLogLn("回单申请失败, 原因:" + tmpStatusText);
                    }
                } else {
                    WriteBankLogLn("回单申请失败, 原因:" + tmpSbMsg.toString());
                }
            } else {
                WriteBankLogLn("回单申请失败, 原因:" + tmpStrRet[1]);
            }
        } else {
            WriteBankLogLn("签名失败, 原因:" + tmpSign[1]);
        }
        return null;
    }
}
